/**
 * Created by gaoyj on 2018/6/27.
 */
window.onload = function (ev) {
    topbar.hide();
};

$('#dataTable').bootstrapTable({
    url: '/UserAnnouncementController/getAnnouncementModel',
    height: $(window).height() - 200,
    pageSize: 30,
    pagination: false,
    pageList: [30, 50, 100],
    clickToSelect: true,
    singleSelect: true,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "rows": res   //数据
        };
    },
    columns: [
        {
            checkbox: true
        }, {
            width: '3px',
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        }, {
            title: '标题',
            field: 'title',
            width: 190,
            align: 'center'
        }
    ]
})
function addTemplate(v) {
    $("#addModalLabel").attr("msg", v);
    if (v == 1) {
        $("#addModalLabel").html("添加模板");
        $("#addForm input,#addForm select,#addForm textarea").val("");
        editor.html("");
    } else {
        $("#addModalLabel").html("修改模板");
        var rows = $('#dataTable').bootstrapTable("getSelections");
        if (rows == null || rows.length === 0) {
            layer.msg("请选择要操作的数据");
            return;
        }
        $("#titleAdd").val(rows[0].title)
        editor.html(rows[0].content)
    }
    $("#templateAddModal").modal("show")
}
$("#addTplSubmit").click(function () {
    var rows = $('#dataTable').bootstrapTable("getSelections");
    var msg = $("#addModalLabel").attr("msg");
    var url="/UserAnnouncementController/addModel";
    var data;
    if(msg==1){
        data={
            title: $.trim($("#titleAdd").val()),
            'content': $.trim($("#contentAdd").val()),
            operator:0
        }
    }else{
        data={
            id:rows[0].id,
            title: $.trim($("#titleAdd").val()),
            'content': $.trim($("#contentAdd").val()),
            operator:1
        }
    }
    $.ajax({
        type: "POST",
        url: url,
        data:data,
        dataType: "json",
        success: function (data) {
            if (data.success) {
                $("#dataTable").bootstrapTable("refresh");
                layer.msg(data.message);
                $("#templateAddModal").modal("hide");
            } else {
                layer.msg(data.message)
            }
        },
        beforeSend: function () {
        }
    });
})
var editor;
var options = {
    cssPath: 'js/kindeditor-4.1.10/plugins/code/prettify.css',
    filterMode: true,
    resizeType: 1,
    allowPreviewEmoticons: false,
    allowImageUpload: true,
    allowFileManager: true,
    uploadJson: '/news/getPicUrl',
    fileManagerJson: '/js/kindeditor-4.1.10/jsp/file_manager_json.jsp',
    afterUpload: function () {
        this.sync();
    },
    afterBlur: function () {
        this.sync();
    },
    width: '100%',
    height: '300px',
    items: [
        'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline']
};
KindEditor
    .ready(function (K) {
        editor = K.create('#contentAdd', options);
    });